﻿using System.Collections.Generic;
using ACompany.OrderProcessing.Infrastructure;
using ACompany.OrderProcessing.Model;
using NUnit.Framework;
using System.Linq;


[TestFixture]
public class CustomerTests
{

    [Test()]
    public void A_Customer_Is_Not_Valid_Unless_He_Has_An_Address()
    {
        Customer aCustomer = new Customer();
        aCustomer.Name = "Mr Test";
        aCustomer.Email = "MrTest@Test.com";

        Assert.IsTrue(ContainsBrokenRules("A Customer must have an address", aCustomer.GetBrokenRules()));
    }

    [Test()]

    public void A_Customers_Email_Address_Has_To_Be_No_Greater_Than_50_Chars_Otherwise_The_Customer_Is_Invalid()
    {
        Customer aCustomer = new Customer();
        aCustomer.Name = "Mr Test";
        aCustomer.Email = "MrTest@Testrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.com";

        Assert.IsTrue(ContainsBrokenRules("Email should be 50 chars or less", aCustomer.GetBrokenRules()));
    }

    [Test()]

    public void A_Customers_Name_Has_To_Be_No_Greater_Than_50_Chars_Otherwise_The_Customer_Is_Invalid()
    {
        Customer aCustomer = new Customer();
        aCustomer.Name = "Mr Test xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        aCustomer.Email = "MrTest@Test.com";

        Assert.IsTrue(ContainsBrokenRules("Name should be 50 chars or less", aCustomer.GetBrokenRules()));
    }

    [Test()]
    public void A_Customer_Is_Valid_If_They_Have_No_Broken_Rules()
    {
        Customer aCustomer = new Customer();
        Address anAddress = new Address("My Street", "My City", "My County", "Post Code");

        aCustomer.Name = "Mr Test";
        aCustomer.Email = "MrTest@test.com";

        aCustomer.Address = anAddress;

        Assert.AreEqual(0, aCustomer.GetBrokenRules().Count);
    }

    private bool ContainsBrokenRules(string ExpectedBrokeRule, IList<BrokenRule> ListOFBrokenRules)
    {

        foreach (BrokenRule brokenRule in ListOFBrokenRules)
        {
            if (ExpectedBrokeRule == brokenRule.Rule)
            {
                return true;
            }
        }

        return false;

    }


}